package com.patchmanage.back.controller;


import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patchmanage.back.mongodb.model.SystemLog;
import com.patchmanage.back.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  日志控制器
 * </p>
 *
 * @author huizhao
 * @since 2021-07-04
 */
@Api(value = " 日志 controller", tags = {"日志 Api"})
@RestController
@RequestMapping("/back/log")
public class SystemLogController {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 分页获取日志列表接口
     *
     * @param dict
     * @return
     */
    @ApiOperation(value = "分页获取日志列表接口", notes = "分页获取日志列表接口")
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Result<Page<SystemLog>> list(@ApiIgnore @RequestBody Dict dict) {
        Criteria criteria = new Criteria();
        Query query = new Query();
        if(StrUtil.isNotBlank(dict.getStr("search"))){
            criteria = Criteria.where("operUserName").regex(dict.getStr("operUserName"));
            query.addCriteria(criteria);
        }

        if(StrUtil.isNotBlank(dict.getStr("operModul"))){
            criteria = Criteria.where("operModul").is(dict.getStr("operModul"));
            query.addCriteria(criteria);
        }

        query.skip(dict.getInt("pageNo") * dict.getInt("pageSize"));
        query.limit(dict.getInt("pageSize"));
        query.with(Sort.by( Sort.Order.desc("createTime")));
        List<SystemLog> systemLogList =  mongoTemplate.find(query, SystemLog.class);

        int count = (int) mongoTemplate.count(query, SystemLog.class);
        //创建分页实体对象
        Page<SystemLog> page = new Page<>();
        //添加每页的集合、数据总条数、总页数
        page.setRecords(systemLogList);
        page.setSize(dict.getInt("pageSize"));
        page.setTotal(count);
        return Result.success(page);
    }

    /**
     * 分页获取策略执行日志接口
     *
     * @param dict
     * @return
     */
    @ApiOperation(value = "分页获取策略执行日志列表接口", notes = "分页获取策略执行日志列表接口")
    @RequestMapping(value = "inventoryLoglist", method = RequestMethod.POST)
    public Result<Page<Map>> inventoryLoglist(@ApiIgnore @RequestBody Dict dict) {
        Criteria criteria = null;
        Query query = new Query();
        if(StrUtil.isNotBlank(dict.getStr("search"))){
            criteria = Criteria.where("strategy_name").regex(".*?"+dict.getStr("search")+".*");
            query.addCriteria(criteria);
        }

        query.skip(dict.getInt("pageNo") * dict.getInt("pageSize"));
        query.limit(dict.getInt("pageSize"));
        query.with(Sort.by( Sort.Order.desc("timestamp")));
        List<Map> list =  mongoTemplate.find(query, Map.class,"strategy_log");

        int count = list.size();
        //创建分页实体对象
        Page<Map> page = new Page<>();
        //添加每页的集合、数据总条数、总页数
        page.setRecords(list);
        page.setSize(dict.getInt("pageSize"));
        page.setTotal(count);
        return Result.success(page);
    }

}
